package com.lc.demo.service.impl;

import com.lc.demo.bean.Address;
import com.lc.demo.mapper.AddressMapper;
import com.lc.demo.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: sakila
 * @Date: 2022/03/07/18:52
 * @Description:
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> selectAddressList(Integer userId) {
        return addressMapper.selectByUserId(userId);

    }

    @Override
    public void addAddress(Address address) {
        addressMapper.insert(address);
    }

    @Override
    public Address findById(Integer id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAddress(Address address) {
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public void addressDefault(Integer id, Integer userId) {
        //将该用户的其他地址设为非默认
        addressMapper.reloadAddressDefault(userId);
        //将当前地址设为默认
        addressMapper.addressDefault(id);
    }

    @Override
    public void delete(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }
}
